Skip to content

[FEAT] Add bitstream module in lib_ccxr #1649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

mrswastik-robot
Copy link
Contributor

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.
  • I have mentioned this change in the changelog.

My familiarity with the project is as follows (check one):

  • I have never used CCExtractor.
  • I have used CCExtractor just a couple of times.
  • I absolutely love CCExtractor, but have not contributed previously.
  • I am an active contributor to CCExtractor.

This PR migrates the bitstream functionality from C to Rust while maintaining backward compatibility by creating a new Rust implementation in lib_ccxr/src/common/bitstream.rs and connecting it to the existing C code through FFI bindings.

@@ -25,6 +24,9 @@ struct bitstream
// increased by the calling function, or not.
unsigned char *_i_pos;
int _i_bpos;
#ifndef DISABLE_RUST
void *rust_bs; // Pointer to Rust's Bitstream struct
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we construct / deconstruct the struct when passing to rust instead of storing a different pointer?

There are other files, where you can see the to_ctype()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaa surely, this should be a better approach rather than having to maintain two structures. I will try to fix it in my next commit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prateekmedia , I have made changes in the bitstream.rs file , can you please review it now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will check this.

@prateekmedia prateekmedia requested a review from Copilot June 21, 2025 09:05
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new bitstream module implemented in Rust and connects it to the existing C code via FFI.

  • Introduces Rust-side bitstream functions and exposes them through the C wrapper
  • Updates C bitstream implementation to delegate to Rust when DISABLE_RUST is not defined
  • Adjusts tests and build configuration to include the new module

Reviewed Changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/rust/wrapper.h Includes the C header for bitstream to expose FFI
src/rust/src/libccxr_exports/mod.rs Registers the bitstream export module
src/rust/lib_ccxr/src/util/bits.rs Refactors parity tests to use assert!
src/rust/lib_ccxr/src/common/mod.rs Adds bitstream to the common mod exports
src/rust/build.rs Links the bitstream library into the build script
src/lib_ccx/cc_bitstream.c Wraps the C implementation to call into Rust
Comments suppressed due to low confidence (3)

src/lib_ccx/cc_bitstream.c:238

  • The function signature returns unsigned char * but the Rust FFI ccxr_next_bytes exports a const uint8_t *. Consider updating this to const unsigned char * to preserve the const qualifier and avoid discarding it.
		bstr->bpos = 8;

src/rust/wrapper.h:14

  • wrapper.h includes the C header for bitstream but does not declare the extern ccxr_* functions provided by the Rust bitstream module, so consumers won’t see the Rust FFI symbols. Consider adding extern declarations for each ccxr_* function in this header.
#include "../lib_ccx/cc_bitstream.h"

src/rust/build.rs:30

  • The bitstream entry may not match the actual C library name produced (likely something like ccx_bitstream or part of the existing ccx library), which could cause linker errors. Verify that the linked library name matches the compiled archive.
        "bitstream",

Comment on lines 8 to 16
/// # Safety
/// This function is unsafe because it:
/// - Dereferences a raw pointer (`bitstream_ptr`) without null checking
/// - Assumes `bitstream_ptr` points to a valid, properly initialized and aligned `bitstream` struct
/// - Assumes the `bitstream` struct has sufficient memory allocated for all fields
/// - Assumes `rust_bitstream` is properly initialized with valid data
/// - Performs pointer arithmetic on `rust_bitstream.data` assuming it points to valid memory
/// - The caller must ensure the `bitstream` struct remains valid for the duration of the operation
/// - The data referenced by `rust_bitstream.data` must remain valid during the operation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep Safety comments concise to 1 or 2 lines at max.

Copy link
Member

@prateekmedia prateekmedia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks.

@ccextractor-bot

This comment was marked as off-topic.

@ccextractor-bot

This comment was marked as off-topic.

@prateekmedia prateekmedia merged commit 81fdecd into CCExtractor:master Jul 6, 2025
18 checks passed
steel-bucket added a commit to steel-bucket/ccextractor that referenced this pull request Jul 29, 2025
* feat: Add bitstream module

* run code formatters

* Run cargo clippy --fix

* Run cargo fmt --all

* refactor: remove rust pointer from C struct

* feat: Add bitstream module

* run code formatters

* Run cargo clippy --fix

* Run cargo fmt --all

* refactor: remove rust pointer from C struct

* Added Bitstream to libccxr_exports

* Minor Formatting Issue

* Bitstream: Removed redundant CType

* bitstream: recommended changes for is_byte_aligned

* bitstream: recommended changes for long comments

* bitstream: comment fix

* bitstream: removed redundant comparism comments

---------

Co-authored-by: Deepnarayan Sett <[email protected]>
Co-authored-by: Deepnarayan Sett <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants